<?php
$cfgThumbnailWidth  = 1024;
$cfgThumbnailHeight = 768;

class thumbnail
{
	var $sourceFile; // We use this file to create the thumbnail
	var $originalFilename; // We use this to get the extension of the filename
	var $destinationDirectory; // The Directory in question
	var $destinationDirectoryFilename; // The destination filename
	
	var $createImageFunction = '';
	var $outputImageFunction = '';
	
	function generate($sourceFile = "", $originalFilename = "", $destinationDirectory = "", $destinationDirectoryFilename = "", $width = -1, $height = -1)
	{
    if (!empty($sourceFile))
      $this->sourceFile = $sourceFile;
    
    if (!empty($originalFilename))
      $this->originalFilename = $originalFilename;
     
    if (!empty($destinationDirectory))
      $this->destinationDirectory = $destinationDirectory;
     
    if (!empty($destinationDirectoryFilename))
      $this->destinationDirectoryFilename = $destinationDirectoryFilename;
    
    if (!empty($width))
      $this->width = $width;
    
    if (!empty($height))
      $this->height = $height;

    list(, $this->extension) = explode('.', $this->originalFilename);
		
		switch (strtolower($this->extension))
		{
			case 'gif' :
				$createImageFunction = 'imagecreatefromgif';
				$outputImageFunction = 'imagegif';
			  break;
			
			case 'png' :
				$createImageFunction = 'imagecreatefrompng';
				$outputImageFunction = 'imagepng';
			  break;
			
			case 'bmp' :
				$createImageFunction = 'imagecreatefromwbmp';
				$outputImageFunction = 'imagewbmp';
			  break;
			
			case 'jpg': case 'jpeg':
				$createImageFunction = 'imagecreatefromjpeg';
				$outputImageFunction = 'imagejpeg';
			  break;
			
			default : 
				exit("Sorry: The format '{$this->extension}' is unsuported");
			  break;
		}
		
		$this->img  = $createImageFunction($this->sourceFile);
		
		list($this->org_width, $this->org_height) = getimagesize($this->sourceFile);
		
		if ($this->height == -1)
			$this->height = round($this->org_height * $this->width / $this->org_width);
		
		if ($this->width == -1)
			$this->width = round($this->org_width * $this->height / $this->org_height);
		
		$this->xoffset = 0;
		$this->yoffset = 0;
		
		$this->img_new = imagecreatetruecolor($this->width, $this->height);	
		
		if ($this->img_new)
		{
			imagecopyresampled($this->img_new, $this->img, 0, 0, $this->xoffset, $this->yoffset, $this->width, $this->height, $this->org_width, $this->org_height);
			
			list($this->newFilename) = explode('.', $this->destinationDirectoryFilename);
			
			$this->fullDestination = ($this->destinationDirectory.'/'.$this->newFilename.'.'.$this->extension);
			
			$outputImageFunction($this->img_new, $this->fullDestination);
		}
		else
			$this->failed = true;
	}
}

function setImageHightWigth($height,$width,$fixWidth,$fixHeight)
{
	$imageParameter = array();
	
	if($width > $fixWidth && $height > $fixHeight)
	{
		if($width > $height)
	  {
	    $imageParameter['newWidth']   = $fixWidth;
	    $imageParameter['newHeight'] = -1;
	  }
	  else
		{
	    $imageParameter['newWidth']   = -1;
	    $imageParameter['newHeight'] = $fixHeight;
	  }
  }
  else if($width > $fixWidth)
  {
    $imageParameter['newWidth']   = $fixWidth;
    $imageParameter['newHeight'] = -1;
  }
  else //$height > $fixHeight
  {
    $imageParameter['newWidth']   = -1;
    $imageParameter['newHeight'] = $fixHeight;
  }
  
  return $imageParameter;
}
?>